import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.NoSuchElementException;
import java.util.Properties;

/**
 * 
 * The main class, where interactive epic fiction begins.
 *
 */
public class BaphiEngine {

	// The mighty data structures that hold everything in the known universe.
	CommodityList commodityList;
	PlayerList playerList;
	PlanetList planetList;
	OpponentList stsOpponentList;
	OpponentList hthOpponentList;
	InventoryList inventoryList;
	ActivityList activityList;
	
	BaphiEngine()
	{
		Properties props = new Properties();
		FileInputStream fis;
		
		try
		{
			fis = new FileInputStream("BaphiEngine.properties");
			props.load(fis);

			commodityList = new CommodityList(readProperty(props, "commodityPath"));
			playerList = new PlayerList(readProperty(props, "playerPath"), commodityList);
			planetList = new PlanetList(readProperty(props, "planetPath"), playerList.size());
			stsOpponentList = new OpponentList(readProperty(props, "stsOpponentPath"));
			hthOpponentList = new OpponentList(readProperty(props, "hthOpponentPath"));
			inventoryList = new InventoryList();
			inventoryList.populate(readProperty(props, "shipInventoryPath"), Inventory.Mounting.Ship, stsOpponentList.size());
			inventoryList.populate(readProperty(props, "personalInventoryPath"), Inventory.Mounting.Personal, hthOpponentList.size());
			activityList = new ActivityList(readProperty(props, "activityPath"), commodityList, planetList, playerList, 
					inventoryList, hthOpponentList, stsOpponentList);
		}
		catch(FileNotFoundException e)
		{
			e.printStackTrace();
			return;
		}
		catch(IOException e)
		{
			e.printStackTrace();
			return;
		}
		catch (NoSuchElementException e)
		{
			e.printStackTrace();
			return;
		}
	}
	
	String readProperty(Properties props, String key) throws NoSuchElementException
	{
		String filePath = props.getProperty(key);
		
		if (filePath == null)
			throw new NoSuchElementException("FATAL: Element \"" + key + "\" not found in properties file.");

		return filePath;
	}
	
	int MainLoop()
	{
		//TODO Put in the UI and all the other stuff that needs to be here.
		return 0;
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args)
	{
		System.out.println("Baphi Interactive Epic Fiction Engine v0.1");
		
		BaphiEngine be = new BaphiEngine();
		be.MainLoop();
	}
	
}

